﻿using BaseUi.ViewModels;
using Ping9719.DeviceCommunication;
using Ping9719.WpfEx;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BaseUi
{
    /// <summary>
    /// ImitateView.xaml 的交互逻辑
    /// </summary>
    public partial class ImitateView : UserControlBase
    {
        bool[] star = {false,false,false,false,false};
        ImitateViewModel viewModel = new ImitateViewModel();
        public ImitateView()
        {
            InitializeComponent();
            this.DataContext = viewModel;
        }

        private void clear(object sender, RoutedEventArgs e)
        {
            viewModel.text = "";
        }

        private void CheckBoxRePrint_Checked(object sender, RoutedEventArgs e)
        {
            string retul = "";
            try
            {
                retul = "";
                var aaa = new Result<string>();
                switch ((sender as CheckBox).Name)
                {

                    case "CheckBoxRePrint5":
                        star[4] = true;
                        break;
                    case "CheckBoxRePrint4":
                        star[3] = true;
                        break;
                    case "CheckBoxRePrint3":
                        star[2] = true;
                        break;
                    case "CheckBoxRePrint2":
                        star[1] = true;
                        break;
                    case "CheckBoxRePrint1":
                        star[0] = true;
                        break;
                }
                for (int i = 0; i<5; i++)
                {
                    if (star[i])
                    {
                        retul = retul + (i+1)+"号抓取";
                    }
                }
                textBoxInfo.AppendText($"{retul}\r\n");
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void CheckBoxRePrint_Unchecked(object sender, RoutedEventArgs e)
        {
            string retul = "";
            try
            {
                retul = "";
                var aaa = new Result<string>();
                switch ((sender as CheckBox).Name)
                {

                    case "CheckBoxRePrint5":
                        star[4] = false;
                        break;
                    case "CheckBoxRePrint4":
                        star[3] = false;
                        break;
                    case "CheckBoxRePrint3":
                        star[2] = false;
                        break;
                    case "CheckBoxRePrint2":
                        star[1] = false;
                        break;
                    case "CheckBoxRePrint1":
                        star[0] = false;
                        break;
                }
                for (int i = 0; i < 5; i++)
                {
                    if (star[i])
                    {
                        retul = retul + (i + 1) + "号丢弃";
                    }
                }
                textBoxInfo.AppendText($"{retul}\r\n");
            }
            catch (Exception ex)
            {
                textBoxInfo.AppendText($"错误：{ex.Message}\r\n");
            }
        }

        private void PLC1(object sender, RoutedEventArgs e)
        {
            string retul = "";
            var plcinfo = LocalStatic.Plc.Read<bool>("轨道1物料到位");
            if (plcinfo.IsSucceed == true && plcinfo.Value == true)
            {
                textBoxInfo.AppendText($"轨道1到料请求确认\r\n");
            }
            else
            {
                textBoxInfo.AppendText($"轨道1到料未请求确认\r\n");
            }
            var plcinfo1 = LocalStatic.Plc.Read<int>("PC轨道1到位序号");
            if (plcinfo1.IsSucceed == true )
            {
                textBoxInfo.AppendText($"PC轨道1到位序号{plcinfo1.Value}\r\n");
            }
            plcinfo = LocalStatic.Plc.Read<bool>("轨道2物料到位");
            if (plcinfo.IsSucceed == true && plcinfo.Value == true)
            {
                textBoxInfo.AppendText($"轨道2到料请求确认\r\n");
            }
            else
            {
                textBoxInfo.AppendText($"轨道2到料未请求确认\r\n");
            }
            plcinfo1 = LocalStatic.Plc.Read<int>("PC轨道2到位序号");
            if (plcinfo1.IsSucceed == true )
            {
                textBoxInfo.AppendText($"PC轨道2到位序号{plcinfo1.Value}\r\n");
            }
            return;
        }

        private void PLC2(object sender, RoutedEventArgs e)
        {
            var data = LocalStatic.Plc.Read<bool>("向PC请求抓取");
            if (data.IsSucceed && data.Value)
            {
                textBoxInfo.AppendText($"PLC向上位机请求抓取\r\n");
            }
            var data2 = LocalStatic.Plc.Read<Int32>("入料搬运夹爪数据");
            if (data2.IsSucceed)
            {
                textBoxInfo.AppendText($"入料搬运夹爪数据{data2.Value}\r\n");
            }
            var data1 = LocalStatic.Plc.Read<Int32>("搬运抓取次数");
            if (data1.IsSucceed)
            {
                textBoxInfo.AppendText($"搬运抓取次数{data1.Value}\r\n");
            }
            return;
        }

        private void PLC3(object sender, RoutedEventArgs e)
        {
            var data = LocalStatic.Plc.Read<bool>("向PC发物料放置到旋转台");
            if (data.IsSucceed)
            {
                textBoxInfo.AppendText($"向上位机发物料放置到旋转台{data.Value}\r\n");
            }
        }

        private void PLC4(object sender, RoutedEventArgs e)
        {
            var data = LocalStatic.Plc.Read<bool>("向PC发模块放入萃盘");
            if (data.IsSucceed)
            {
                textBoxInfo.AppendText($"向PC发模块放5个入萃盘{data.Value}\r\n");
            }
            var data1 = LocalStatic.Plc.Read<bool>("PC收到模块放入萃盘");
            if (data1.IsSucceed && !data1.Value)
            {
                textBoxInfo.AppendText($"PC收到模块放入萃盘{data1.Value}\r\n");
            }
        }

        private void PLC5(object sender, RoutedEventArgs e)
        {
            var data = LocalStatic.Plc.Read<bool>("触发视觉");
            if (data.IsSucceed && data.Value)
            {
                textBoxInfo.AppendText($"触发视觉{data.Value}\r\n");
            }
            var data1 = LocalStatic.Plc.Read<bool>("视觉拍照完成");
            if (data1.IsSucceed && !data1.Value)
            {
                textBoxInfo.AppendText($"视觉拍照完成{data1.Value}\r\n");
            }
        }

        private void PLC6(object sender, RoutedEventArgs e)
        {
            var data = LocalStatic.Plc.Read<bool>("向PC发出堆料请求");
            if (data.IsSucceed && data.Value)
            {
                textBoxInfo.AppendText($"向PC发出堆料请求{data.Value}\r\n");
            }
            var data1 = LocalStatic.Plc.Read<bool>("PC允许堆料");
            if (data1.IsSucceed && !data1.Value)
            {
                textBoxInfo.AppendText($"向PC发出堆料请求{data1.Value}\r\n");
            }
        }

        private void PLC7(object sender, RoutedEventArgs e)
        {
            var data = LocalStatic.Plc.Read<bool>("向PC发出整堆出料");
            if (data.IsSucceed && data.Value)
            {
                textBoxInfo.AppendText($"向PC发出整堆出料请求{data.Value}\r\n");
            }
            var data1 = LocalStatic.Plc.Read<bool>("PC允许整堆出料");
            if (data1.IsSucceed && !data1.Value)
            {
                textBoxInfo.AppendText($"PC允许整堆出料{data1.Value}\r\n");
            }
        }

        private void PLC8(object sender, RoutedEventArgs e)
        {
            var data = LocalStatic.Plc.Read<bool>("PC查询数据抓完");
            if (data.IsSucceed)
            {
                textBoxInfo.AppendText($"PC查询数据抓完请求{data.Value}\r\n");
            }
            var data1 = LocalStatic.Plc.Read<bool>("PC查询OK");
            if (data1.IsSucceed && !data1.Value)
            {
                textBoxInfo.AppendText($"PC查询数据抓完{data1.Value}\r\n");
            }
        }
        private void PLC9(object sender, RoutedEventArgs e)
        {
            var data = LocalStatic.Plc.Read<Int32>("设置堆料盘数");
            if (data.IsSucceed)
            {
                textBoxInfo.AppendText($"堆料盘数{data.Value}\r\n");
            }
        }
        private void SWJ1(object sender, RoutedEventArgs e)
        {
            textBoxInfo.AppendText($"发送轨道到位序号\r\n");
            LocalStatic.Plc.Write("PC轨道1到位序号", 1);
            var plcinfo1 = LocalStatic.Plc.Read<int>("PC轨道1到位序号");
            if (plcinfo1.IsSucceed == true)
            {
                textBoxInfo.AppendText($"PLC轨道1到位序号{plcinfo1.Value}\r\n");
            }
            LocalStatic.Plc.Write("PC轨道2到位序号", 2);
            plcinfo1 = LocalStatic.Plc.Read<int>("PC轨道2到位序号");
            if (plcinfo1.IsSucceed == true)
            {
                textBoxInfo.AppendText($"PLC轨道2到位序号{plcinfo1.Value}\r\n");
            }
        }

        private void SWJ2(object sender, RoutedEventArgs e)
        {
            textBoxInfo.AppendText($"发送轨道到位序号\r\n");
            LocalStatic.Plc.Write("PC轨道1到位序号", 2);
            var plcinfo1 = LocalStatic.Plc.Read<int>("PC轨道1到位序号");
            if (plcinfo1.IsSucceed == true)
            {
                textBoxInfo.AppendText($"PLC轨道1到位序号{plcinfo1.Value}\r\n");
            }
            LocalStatic.Plc.Write("PC轨道2到位序号", 1);
            plcinfo1 = LocalStatic.Plc.Read<int>("PC轨道2到位序号");
            if (plcinfo1.IsSucceed == true)
            {
                textBoxInfo.AppendText($"PLC轨道2到位序号{plcinfo1.Value}\r\n");
            }
        }

        private void SWJ3(object sender, RoutedEventArgs e)
        {
            int value = 0;
            for (int i = 0; i < star.Length; i++)
            {
                if (star[i])
                    value = value + (0x01 << i);
            }
            LocalStatic.Plc.Write("入料搬运夹爪数据", value);
            var data2 = LocalStatic.Plc.Read<Int32>("入料搬运夹爪数据");
            if (data2.IsSucceed)
            {
                textBoxInfo.AppendText($"入料搬运夹爪数据{data2.Value}\r\n");
            }
        }

        private void SWJ4(object sender, RoutedEventArgs e)
        {
            LocalStatic.Plc.Write("收到PC放置确认", true);
            var data1 = LocalStatic.Plc.Read<bool>("收到PC放置确认");
            if (data1.IsSucceed )
            {
                textBoxInfo.AppendText($"旋转放置确认{data1.Value}\r\n");
            }
        }

        private void SWJ5(object sender, RoutedEventArgs e)
        {
            LocalStatic.Plc.Write("PC收到模块放入萃盘", true);
            var data1 = LocalStatic.Plc.Read<bool>("PC收到模块放入萃盘");
            if (data1.IsSucceed && !data1.Value)
            {
                textBoxInfo.AppendText($"tray盘放置确认{data1.Value}\r\n");
            }
        }

        private void SWJ6(object sender, RoutedEventArgs e)
        {

            LocalStatic.Plc.Write("视觉拍照完成", true);
            var data1 = LocalStatic.Plc.Read<bool>("视觉拍照完成");
            if (data1.IsSucceed)
            {
                textBoxInfo.AppendText($"视觉确认{data1.Value}\r\n");
            }
        }

        private void SWJ7(object sender, RoutedEventArgs e)
        {
            string vision = LocalStatic.Vision.SendMes("code");
            string[] visiong = vision.Trim().Split(';');
            for (int i=0;i<visiong.Length;i++)
            {
                textBoxInfo.AppendText($"视觉第{i + 1}条数据：{visiong[i]}\r\n");
            }
        }

        private void SWJ8(object sender, RoutedEventArgs e)
        {

            LocalStatic.Plc.Write("PC允许堆料", true);
            var data1 = LocalStatic.Plc.Read<bool>("PC允许堆料");
            if (data1.IsSucceed )
            {
                textBoxInfo.AppendText($"入堆确认{data1.Value}\r\n");
            }
        }

        private void SWJ9(object sender, RoutedEventArgs e)
        {

            LocalStatic.Plc.Write("PC允许整堆出料", true);
            var data1 = LocalStatic.Plc.Read<bool>("PC允许整堆出料");
            if (data1.IsSucceed)
            {
                textBoxInfo.AppendText($"下料确认{data1.Value}\r\n");
            }
        }

        private void SWJ10(object sender, RoutedEventArgs e)
        {
            LocalStatic.Plc.Write("PC查询OK", true);

            var data1 = LocalStatic.Plc.Read<bool>("PC查询OK");
            if (data1.IsSucceed)
            {
                textBoxInfo.AppendText($"供料处理完确认{data1.Value}\r\n");
            }
        }
        private void SWJ11(object sender, RoutedEventArgs e)
        {
            LocalStatic.Plc.Write("设置堆料盘数", int.Parse(ceng.Text));

            var data1 = LocalStatic.Plc.Read<Int32>("设置堆料盘数");
            if (data1.IsSucceed)
            {
                textBoxInfo.AppendText($"供料处理完确认{data1.Value}\r\n");
            }
        }
        private void textchange(object sender, TextChangedEventArgs e)
        {
            var nam = (sender as TextBox);
            nam.ScrollToEnd();
        }

    }
}
